import React from 'react';
import { routerRedux } from 'dva/router';
import pathToRegexp from 'path-to-regexp';
import { Modal } from 'antd-mobile';
import { getUploadToken, submitPhysicianAuth } from '../services/register';
import { getLocalStorage } from '../utils/storage';

export default {
  namespace: 'physicianAuth',
  state: {
    doctorInfoLoading: false,
    selectedLoc: {
      name: '',
      address: '',
      citycode: '',
      lat: 0,
      lng: 0,
    },
    doctName: '',
    idCard: '',
    hospital: '',
    deptName: '',
    deptCode: '',
    titleName: '',
    titleCode: '',
    specialization: '',
    image: '',
    buttonStatus: true,
    imgSrc: '',
    imgSrc2: '',
    file: {},
    uploadToken: '',
  },
  reducers: {
    setFile(state, { payload }) {
      return { ...state, file: payload.file };
    },
    setImage2(state, { payload }) {
      return { ...state, imgSrc2: payload.imgSrc };
    },
    setImage(state, { payload }) {
      return { ...state, imgSrc: payload.imgSrc };
    },
    fetchUploadTokenSuccess(state, { payload }) {
      return { ...state, uploadToken: payload };
    },
    selectImage(state, { payload }) {
      if (state.doctName !== '' && state.idCard !== '' && state.selectedLoc.name !== '' && state.deptCode !== '' && state.specialization !== '') {
        if (payload.image !== '') {
          return {
            ...state,
            image: `http://memberdata.jiukangguoji.cn/${payload.key}`,
            buttonStatus: false,
          };
        } else {
          return {
            ...state,
            image: `http://memberdata.jiukangguoji.cn/${payload.key}`,
            buttonStatus: true,
          };
        }
      } else {
        return {
          ...state,
          image: `http://memberdata.jiukangguoji.cn/${payload.key}`,
          buttonStatus: true,
        };
      }
    },
    selectLocSuccess(state, { payload }) {
      if (state.image !== '' && state.doctName !== '' && state.idCard !== '' && state.deptCode !== '' && state.specialization !== '') {
        if (payload.name !== '') {
          return { ...state, buttonStatus: false, selectedLoc: payload };
        } else {
          return { ...state, buttonStatus: true, selectedLoc: payload };
        }
      } else {
        return { ...state, buttonStatus: true, selectedLoc: payload };
      }
    },
    bindDoctorName(state, { payload }) {
      if (state.image !== '' && state.idCard !== '' && state.selectedLoc.name !== '' && state.deptCode !== '' && state.specialization !== '') {
        if (payload.doctName !== '') {
          return { ...state, buttonStatus: false, ...payload };
        } else {
          return { ...state, buttonStatus: true, ...payload };
        }
      } else {
        return { ...state, buttonStatus: true, ...payload };
      }
    },
    bindIdCard(state, { payload }) {
      if (state.image !== '' && state.doctName !== '' && state.selectedLoc.name !== '' && state.deptCode !== '' && state.specialization !== '') {
        if (payload.idCard !== '') {
          return { ...state, buttonStatus: false, ...payload };
        } else {
          return { ...state, buttonStatus: true, ...payload };
        }
      } else {
        return { ...state, buttonStatus: true, ...payload };
      }
    },
    showDoctorInfoLoading(state) {
      return {
        ...state,
        doctorInfoLoading: true,
      };
    },
    hideDoctorInfoLoading(state) {
      return {
        ...state,
        doctorInfoLoading: false,
      };
    },
    selectDeptSuccess(state, { payload }) {
      if (state.image !== '' && state.doctName !== '' && state.idCard !== '' && state.selectedLoc.name !== '' && state.specialization !== '') {
        if (payload.deptCode !== '') {
          return {
            ...state,
            deptName: payload.name,
            deptCode: payload.code,
            buttonStatus: false,
          };
        } else {
          return {
            ...state,
            deptName: payload.name,
            buttonStatus: true,
          };
        }
      } else {
        return {
          ...state,
          deptName: payload.name,
          deptCode: payload.code,
          buttonStatus: true,
        };
      }
    },
    selectSpecialSuccess(state, { payload }) {
      if (state.image !== '' && state.doctName !== '' && state.idCard !== '' && state.selectedLoc.name !== '' && state.deptCode !== '') {
        if (payload.specialization !== '') {
          return {
            ...state,
            specialization: payload.specialization,
            buttonStatus: false,
          };
        } else {
          return {
            ...state,
            specialization: payload.specialization,
            buttonStatus: true,
          };
        }
      } else {
        return {
          ...state,
          specialization: payload.specialization,
          buttonStatus: true,
        };
      }
    },
    selectDoctorLevelSuccess(state, { payload }) {
      if (state.image !== '' && state.doctName !== '' && state.idCard !== '' && state.selectedLoc.name !== '' && state.deptCode !== '' && state.specialization !== '') {
        if (payload.titleCode !== '') {
          return {
            ...state,
            titleName: payload.name,
            titleCode: payload.code,
            buttonStatus: false,
          };
        } else {
          return {
            ...state,
            titleName: payload.name,
            titleCode: payload.code,
            buttonStatus: true,
          };
        }
      } else {
        return {
          ...state,
          titleName: payload.name,
          titleCode: payload.code,
          buttonStatus: true,
        };
      }
    },
  },
  effects: {
    *fetchUploadToken({ payload }, { call, put }) {
      const { data } = yield call(getUploadToken, payload);

      yield put({
        type: 'fetchUploadTokenSuccess',
        payload: data.token,
      });
    },
    *saveDoctorInfo({
      payload,
    }, { put, call }) {
      yield put({ type: 'showDoctorInfoLoading' });
      const { err, data } = yield call(submitPhysicianAuth, payload);
      if (!err) {
        if (!data.isBind) {
          yield put(routerRedux.push('/uploadDoctorImage'));
        } else {
          Modal.alert(`姓名和身份证已被${data.bindUserMobile}绑定`);
        }
      } else {
        err.json.then((errJson) => {
          const title = (
            <span style={{ fontSize: '0.25rem', color: '#666', textAlign: 'left' }}>提示：军人、特工、干部等职业会导致身份信息无法查询，如果您是以上职业请返回页面勾选【我是特殊职业】后重试；其他原因请联系客服。</span>
        );
          if (errJson.errCode === '100402' || errJson.errCode === 100402) {
            Modal.alert('身份信息不正确',
            title, [
              { text: '联系客服',
                onPress: () => {
                  window.location.href = 'tel:010-64010-566';
                },
                style: { color: '#108ee9' },
              },
              { text: '我知道了',
                // onPress: () => {
                //   window.location.href = '/showqrcode.html';
                // },
                // style: { fontWeight: 'bold' }
              },
            ]);
          } else {
            alert(errJson.errMessage);
          }
        });
      }
      yield put({ type: 'hideDoctorInfoLoading' });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        const match = pathToRegexp('/physicianAuth').exec(location.pathname);
        if (match) {
          const token = getLocalStorage('access_token');
          if (token) {
            dispatch({
              type: 'fetchUploadToken',
              payload: {
                token,
                bucket: 'memberdata',
                insertOnly: 0,
              },
            });
          }
        }
      });
    },
  },
};
